﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using CKS.API.Logging;

namespace CKSInstrumentationSandboxTest.SandboxLoggerAdmin
{
    [ToolboxItem(false)]
    public partial class SandboxLoggerAdmin : System.Web.UI.WebControls.WebParts.WebPart
    {
        CKSSandboxLogger logger = null;

        protected override void OnInit(EventArgs e)
        {
            logger = new CKSSandboxLogger(SPContext.Current.Site.RootWeb, false);
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlLoggerLevel.DataSource = Enum.GetNames(typeof(LoggingSeverity));
                ddlLoggerLevel.DataBind();
                ddlLoggerLevel.SelectedIndex = (int)Enum.Parse(typeof(LoggingSeverity), logger.GetLogLevel().ToString());
            }


        }

        protected void btnSetLevel_Click(object sender, EventArgs e)
        {
            int selectedLevel = (int)Enum.Parse(typeof(LoggingSeverity), ddlLoggerLevel.SelectedValue);
            if (null != SPContext.Current)
            {
                SPFolder rootFolder = SPContext.Current.Site.RootWeb.Lists.TryGetList("SandboxLog").RootFolder;
                rootFolder.SetProperty("CKSAPI_LogLevel", selectedLevel);
                rootFolder.Update();

            }


        }
    }
}
